/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

/**
 * This class translate the learning Design element
 *
 */
public class LearningDesignTranslator {

    /**
     * Creates a new instance of LearningDesignTranslator    
     * @param lnode The learning design node of the Dom structure
     * @param ont The ontology
     * @param nsName The namespace   
     */
    public LearningDesignTranslator(Node lnode, OntModel ont, String nsName) {

        OntClass LearningDesign =
                ont.getOntClass(nsName + "Learning-Design");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        DatatypeProperty uriProperty =
                ont.getDatatypeProperty(nsName + "uri");
        DatatypeProperty sequenceUsed =
                ont.getDatatypeProperty(nsName + "sequence-used");
        DatatypeProperty version =
                ont.getDatatypeProperty(nsName + "version");
        DatatypeProperty level =
                ont.getDatatypeProperty(nsName + "level");

        Individual inst =
                null;

        try {
            //find te last instance
            lastInstanceSLDTranslator lastInst =
                    new lastInstanceSLDTranslator();
            String instance =
                    "learning_design_instance_" +
                    (String.valueOf(lastInst.lastInstance(LearningDesign)));
            Individual instLd =
                    ont.createIndividual(nsName + instance, LearningDesign);

            /* Get the elements */
            NodeList children =
                    lnode.getChildNodes();
            if (children != null) {
                for (int i =
                        0; i < children.getLength(); i++) {
                    if (children.item(i).getNodeName().compareTo("imsld:title") ==
                            0) {
                        RDFNode titleValue =
                                ont.createTypedLiteral(children.item(i).
                                getTextContent());
                        instLd.setPropertyValue(title, titleValue);

                    }
                    if (children.item(i).getNodeName().compareTo("imsld:learning-objectives") ==
                            0) {
                        LearningObjectivesTranslator dlo =
                                new LearningObjectivesTranslator(children.item(i),
                                ont, inst, instLd, nsName);
                    }
                    if (children.item(i).getNodeName().compareTo("imsld:prerequisites") ==
                            0) {
                        PrerequisitesTranslator dpre =
                                new PrerequisitesTranslator(children.item(i),
                                ont, null, instLd, nsName);
                    }
                    if (children.item(i).getNodeName().compareTo("imsld:components") ==
                            0) {
                        ComponentsTranslator dcomp =
                                new ComponentsTranslator(children.item(i), ont,
                                instLd, nsName);
                    }
                    if (children.item(i).getNodeName().compareTo("imsld:method") ==
                            0) {
                        MethodTranslator dcomp =
                                new MethodTranslator(children.item(i), ont,
                                instLd, nsName);
                    }

                }
                /* Get the attributes */
                NamedNodeMap attrs =
                        lnode.getAttributes();
                for (int i =
                        0; i < attrs.getLength(); i++) {
                    Node attr =
                            attrs.item(i);
                    if (attr.getNodeName().trim().compareTo("identifier") == 0) {
                        TypeMapper tm =
                                TypeMapper.getInstance();
                        RDFDatatype overType =
                                tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                        RDFNode identifierValue =
                                ont.createTypedLiteral(attr.getNodeValue().trim(),
                                overType);
                        instLd.setPropertyValue(identifier, identifierValue);
                    }
                    if (attr.getNodeName().compareTo("uri") == 0) {
                        TypeMapper tm =
                                TypeMapper.getInstance();
                        RDFDatatype overType =
                                tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#anyURI");
                        RDFNode uriValue =
                                ont.createTypedLiteral(attr.getNodeValue().trim(),
                                overType);
                        instLd.setPropertyValue(uriProperty, uriValue);
                    }
                    if (attr.getNodeName().compareTo("sequence-used") == 0) {
                        RDFNode sequenceValue;
                        if (attr.getNodeValue().compareTo("true") == 0) {
                            sequenceValue =
                                    ont.createTypedLiteral(true);
                        } else {
                            sequenceValue =
                                    ont.createTypedLiteral(false);
                        }

                        instLd.setPropertyValue(sequenceUsed, sequenceValue);
                    }
                    if (attr.getNodeName().compareTo("version") == 0) {
                        RDFNode versionValue =
                                ont.createTypedLiteral(attr.getNodeValue());
                        instLd.setPropertyValue(version, versionValue);
                    }
                    if (attr.getNodeName().compareTo("level") == 0) {
                        RDFNode levelValue =
                                ont.createTypedLiteral(attr.getNodeValue().trim());
                        instLd.setPropertyValue(level, levelValue);

                    }
                }//end-for           
            }//end-if

        } catch (Exception ex) {

            System.out.println("error" + ex.toString());
            ex.printStackTrace();
        }

    }
}
